package cz.proks.easyhelpdesk.gwt.ui.widget;

import java.util.List;

import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.cell.client.NumberCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SingleSelectionModel;

import cz.proks.easyhelpdesk.api.model.Category;
import cz.proks.easyhelpdesk.api.model.Helpdesk;
import cz.proks.easyhelpdesk.gwt.HelpdeskMessages;
import cz.proks.easyhelpdesk.gwt.event.CategoryDeleteEvent;
import cz.proks.easyhelpdesk.gwt.event.CategorySaveEvent;
import cz.proks.easyhelpdesk.gwt.event.CategorySaveEvent.Handler;
import cz.proks.easyhelpdesk.gwt.event.HasCategoryDeleteHandlers;
import cz.proks.easyhelpdesk.gwt.event.HasCategorySaveHandlers;
import cz.proks.easyhelpdesk.gwt.keyprovider.KeyProvider;

public class CategoriesSettingsWidget extends Composite implements RequiresErase, HasCategorySaveHandlers, HasCategoryDeleteHandlers {

    private static CategoriesSettingsWidgetUiBinder uiBinder = GWT.create(CategoriesSettingsWidgetUiBinder.class);
    
    @UiField HelpdeskMessages i18n;
    @UiField(provided=true) CellTable<Category> table = new CellTable<Category>();    
    @UiField TextBox newCategoryLabelBox;
    @UiField RedButton newCategorySaveButton;

    interface CategoriesSettingsWidgetUiBinder extends UiBinder<Widget, CategoriesSettingsWidget> {
    }

    private Helpdesk helpdesk;
    
    private final KeyProvider<Category> keyProvider = new KeyProvider<Category>();
    private final ListDataProvider<Category> dataProvider = new ListDataProvider<Category>(keyProvider);    

    public CategoriesSettingsWidget() {
        initWidget(uiBinder.createAndBindUi(this));
    }
    
    @Override
    protected void initWidget(Widget widget) {     
        initTable();
        super.initWidget(widget);        
    }                    

    @Override
    public HandlerRegistration addCategorySaveHandler(Handler handler) {
        return addHandler(handler, CategorySaveEvent.TYPE);
    }
    
    @Override
    public HandlerRegistration addCategoryDeleteHandler(cz.proks.easyhelpdesk.gwt.event.CategoryDeleteEvent.Handler handler) {
        return addHandler(handler, CategoryDeleteEvent.TYPE);
    }
    
    @Override
    public void onErase() {
        newCategoryLabelBox.setText("");        
    }

    public List<Category> getEntities() {
        return dataProvider.getList();
    }
    
    public void setHelpdesk(Helpdesk helpdesk) {
        this.helpdesk = helpdesk;
    }
    
    public String getNewCategoryLabel() {
        return newCategoryLabelBox.getText();
    }
    
    public void setNewCategoryLabel(String label) {
        this.newCategoryLabelBox.setText(label == null ? "" : label);
    }
    
    private void initTable() {
        table.setWidth("100%");
        
        final SingleSelectionModel<Category> selection = new SingleSelectionModel<Category>(keyProvider);
        
        table.setSelectionModel(selection);
        dataProvider.addDataDisplay(table);
        
        initTableColumns();
    }
    
    private void initTableColumns() {
        Column<Category, Number> idColumn = new Column<Category, Number>(new NumberCell()) {
            @Override
            public Number getValue(Category object) {
                return object.getId();
            }
        };
        
        Column<Category, String> labelColumn = new Column<Category, String>(new TextCell()) {
            
            @Override
            public String getValue(Category object) {
                return object.getLabel();
            }
        };
        
        Column<Category, String> deleteColumn = new Column<Category, String>(new ButtonCell()) {
            @Override
            public String getValue(Category object) {
                return "Delete";
            }                        
        };    
        
        deleteColumn.setFieldUpdater(new FieldUpdater<Category, String>() {
            
            @Override
            public void update(int index, Category object, String value) {
                CategoryDeleteEvent.fireEvent(CategoriesSettingsWidget.this, object);
            }
        });
        
        table.addColumn(idColumn, i18n.settingsCategoryIdColumn());
        table.addColumn(labelColumn, i18n.settingsCategoryLabelColumn());        
        table.addColumn(deleteColumn, i18n.settingsCategoryDeleteColumn());                
    }
    
    private Category getCategory() {
        Category category = new Category();
        
        category.setHelpdeskId(helpdesk != null ? helpdesk.getId() : null);
        category.setLabel(getNewCategoryLabel());
        
        return category;
    }

    @UiHandler("newCategorySaveButton")
    void onNewCategorySaveButtonClick(ClickEvent event) {
        event.preventDefault();
        CategorySaveEvent.fireEvent(this, getCategory());              
    }

}